home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 April / enter-2004-04.iso / files / httrack-3.30.exe / {app} / src / htsindex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-08-28  |  14.6 KB  |  487 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsindex.c                                             */
  34. /*       keyword indexing system (search index)                 */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "htsindex.h"
  42. #include "htsglobal.h"
  43. #include "htslib.h"
  44.  
  45. #if HTS_MAKE_KEYWORD_INDEX
  46. #include "htshash.h"
  47. #include "htsinthash.h"
  48.  
  49.  
  50. /* Keyword Indexer Parameters */
  51.  
  52. // Maximum length for a keyword
  53. #define KEYW_LEN             50
  54. // Minimum length for a keyword - MUST NOT BE NULL!!!
  55. #define KEYW_MIN_LEN         3
  56. // What characters to accept? - MUST NOT BE EMPTY AND MUST NOT CONTAIN THE SPACE (32) CHARACTER!!!
  57. #define KEYW_ACCEPT          "abcdefghijklmnopqrstuvwxyz0123456789-_."
  58. // Convert A to a, and so on.. to avoid case problems in indexing
  59. // This can be a generic table, containing characters that are in fact not accepted by KEYW_ACCEPT
  60. // MUST HAVE SAME SIZES!!
  61. #define KEYW_TRANSCODE_FROM  (\
  62.                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  63.                                "αΓΣ" \
  64.                                "└┬─" \
  65.                                "ΘΦΩδ" \
  66.                                "╚╚╩╦" \
  67.                                "∞ε∩" \
  68.                                "╠╬╧" \
  69.                                "≥⌠÷" \
  70.                                "╥╘╓" \
  71.                                "∙√ⁿ" \
  72.                                "┘█▄" \
  73.                                " " \
  74.                              )
  75. #define KEYW_TRANSCODE_TO    ( \
  76.                                "abcdefghijklmnopqrstuvwxyz" \
  77.                                "aaa" \
  78.                                "aaa" \
  79.                                "eeee" \
  80.                                "eeee" \
  81.                                "iii" \
  82.                                "iii" \
  83.                                "ooo" \
  84.                                "ooo" \
  85.                                "uuu" \
  86.                                "uuu" \
  87.                                "y" \
  88.                              )
  89. // These (accepted) characters will be ignored at begining of a keyword
  90. #define KEYW_IGNORE_BEG       "-_."
  91. // These (accepted) characters will be stripped if at the end of a keyword
  92. #define KEYW_STRIP_END       "-_."
  93. // Words begining with these (accepted) characters will be ignored
  94. #define KEYW_NOT_BEG         "0123456789"
  95. // Treat these characters as space characters - MUST NOT BE EMPTY!!!
  96. #define KEYW_SPACE           " ',;:!?\"\x0d\x0a\x09\x0b\x0c"
  97. // Common words (the,for..) detector
  98. // If a word represents more than KEYW_USELESS1K (%1000) of total words, then ignore it
  99. // 5 (0.5%)
  100. #define KEYW_USELESS1K       5
  101. // If a word is present in more than KEYW_USELESS1KPG (%1000) pages, then ignore it
  102. // 800 (80%)
  103. #define KEYW_USELESS1KPG     800
  104. // This number will be reduced by index hit for sorting purpose
  105. // leave it as it is here if you don't REALLY know what you are doing
  106. // Yes, I may be the only person, maybe
  107. #define KEYW_SORT_MAXCOUNT 999999999
  108.  
  109. /* End of Keyword Indexer Parameters */
  110.  
  111. int strcpos(char* adr,char c);
  112. int mystrcmp(const void* _e1,const void* _e2);
  113.  
  114. // Global variables
  115. int hts_index_init=1;
  116. int hts_primindex_size=0;
  117. FILE* fp_tmpproject=NULL;
  118. int hts_primindex_words=0;
  119.  
  120. #endif
  121.  
  122. /* 
  123.   Init index 
  124. */
  125. void index_init(const char* indexpath) {
  126. #if HTS_MAKE_KEYWORD_INDEX
  127.   /* remove(concat(indexpath,"index.txt")); */
  128.   hts_index_init=1;
  129.   hts_primindex_size=0;
  130.   hts_primindex_words=0;
  131.   fp_tmpproject=tmpfile();
  132. #endif
  133. }
  134.  
  135.  
  136. /* 
  137.    Indexing system
  138.    A little bit dirty, (quick'n dirty, in fact)
  139.    But should be okay on most cases
  140.    Tags and javascript handled (ignored)
  141. */
  142. int index_keyword(const char* html_data,LLint size,const char* mime,const char* filename,const char* indexpath) {
  143. #if HTS_MAKE_KEYWORD_INDEX
  144.   int intag=0,inscript=0,incomment=0;
  145.   char keyword[KEYW_LEN+32];
  146.   int i=0;
  147.   //
  148.   int WordIndexSize=1024;
  149.   inthash WordIndexHash=NULL;
  150.   FILE *tmpfp=NULL;
  151.   //
  152.  
  153.   // Check parameters
  154.   if (!html_data)
  155.     return 0;
  156.   if (!size)
  157.     return 0;
  158.   if (!mime)
  159.     return 0;
  160.   if (!filename)
  161.     return 0;
  162.  
  163.   // Init ?
  164.   if (hts_index_init) {
  165.     remove(concat(indexpath,"index.txt"));
  166.     remove(concat(indexpath,"sindex.html"));
  167.     hts_index_init=0;
  168.   }
  169.  
  170.   // Check MIME type
  171.   if (strfield2(mime,"text/html")) {
  172.     inscript=0;
  173.   } 
  174.   // FIXME - temporary fix for image/svg+xml (svg)
  175.   // "IN XML" (html like, in fact :) )
  176.   else if (
  177.     (strfield2(mime,"image/svg+xml"))
  178.     ||
  179.     (strfield2(mime,"image/svg-xml"))
  180.     ) {
  181.     inscript=0;
  182.   }
  183.   else if (
  184.     (strfield2(mime,"application/x-javascript"))
  185.     || (strfield2(mime,"text/css"))
  186.     ) {
  187.     inscript=1;
  188.   //} else if (strfield2(mime, "text/vnd.wap.wml")) {   // humm won't work in many cases
  189.   //  inscript=0;
  190.   } else
  191.     return 0;
  192.  
  193.   // Temporary file
  194.   tmpfp = tmpfile();
  195.   if (!tmpfp)
  196.     return 0;
  197.  
  198.   // Create hash structure
  199.   // Hash tables rulez da world!
  200.   WordIndexHash=inthash_new(WordIndexSize);
  201.   if (!WordIndexHash)
  202.     return 0;
  203.  
  204.   // Start indexing this page
  205.   keyword[0]='\0';
  206.   while(i<size) {
  207.     if (strfield(html_data + i , "<script")) {
  208.       inscript=1;
  209.     } 
  210.     else if (strfield(html_data + i , "<!--")) {
  211.       incomment=1;
  212.     }
  213.     else if (strfield(html_data + i , "</script")) {
  214.       if (!incomment)
  215.         inscript=0;
  216.     } 
  217.     else if (strfield(html_data + i , "-->")) {
  218.       incomment=0;
  219.     }
  220.     else if (html_data[i]=='<') {
  221.       if (!inscript)
  222.         intag=1;
  223.     }    
  224.     else if (html_data[i]=='>') {
  225.       intag=0;
  226.     }    
  227.     else {    
  228.       // Okay, parse keywords
  229.       if ( (!inscript) && (!incomment) && (!intag) ) {
  230.         char cchar=html_data[i];
  231.         int pos;
  232.         int len=strlen(keyword);
  233.         
  234.         // Replace (ignore case, and so on..)
  235.         if ((pos=strcpos(KEYW_TRANSCODE_FROM,cchar))>=0)
  236.           cchar=KEYW_TRANSCODE_TO[pos];
  237.         
  238.         if (strchr(KEYW_ACCEPT,cchar)) {
  239.           /* Ignore some characters at begining */
  240.           if ((len>0) || (!strchr(KEYW_IGNORE_BEG,cchar))) {
  241.             keyword[len++]=cchar;
  242.             keyword[len]='\0';
  243.           }
  244.         } else if ( (strchr(KEYW_SPACE,cchar)) || (!cchar) ) {
  245.  
  246.  
  247.           /* Avoid these words */
  248.           if (len>0) {
  249.             if (strchr(KEYW_NOT_BEG,keyword[0])) {
  250.               keyword[(len=0)]='\0';
  251.             }
  252.           }
  253.  
  254.           /* Strip ending . and so */
  255.           {
  256.             int ok=0;
  257.             while((len=strlen(keyword)) && (!ok)) {
  258.               if (strchr(KEYW_STRIP_END,keyword[len-1])) {      /* strip it */
  259.                 keyword[len-1]='\0';
  260.               } else
  261.                 ok=1;
  262.             }
  263.           }
  264.           
  265.           /* Store it ? */
  266.           if (len >= KEYW_MIN_LEN ) {
  267.             hts_primindex_words++;
  268.             if (inthash_inc(WordIndexHash,keyword)) {   /* added new */
  269.               fprintf(tmpfp,"%s\n",keyword);
  270.             }
  271.           }
  272.           keyword[(len=0)]='\0';
  273.         } else      /* Invalid */
  274.           keyword[(len=0)]='\0';
  275.  
  276.         if (len>KEYW_LEN) {
  277.           keyword[(len=0)]='\0';
  278.         }
  279.       }
  280.       
  281.     }
  282.     
  283.     i++;
  284.   }
  285.  
  286.   // Reset temp file
  287.   fseek(tmpfp,0,SEEK_SET);
  288.  
  289.   // Process indexing for this page
  290.   {
  291.     //FILE* fp=NULL;
  292.     //fp=fopen(concat(indexpath,"index.txt"),"ab");
  293.     if (fp_tmpproject) {
  294.       while(!feof(tmpfp)) {
  295.         char line[KEYW_LEN + 32];
  296.         linput(tmpfp,line,KEYW_LEN + 2);
  297.         if (strnotempty(line)) {
  298.           unsigned long int e=0;
  299.           if (inthash_read(WordIndexHash,line,&e)) {
  300.             //if (e) {
  301.             char savelst[HTS_URLMAXSIZE*2];
  302.             e++;          /* 0 means "once" */
  303.             
  304.             if (strncmp((const char*)fslash((char*)indexpath),filename,strlen(indexpath))==0)  // couper
  305.               strcpybuff(savelst,filename+strlen(indexpath));
  306.             else
  307.               strcpybuff(savelst,filename);
  308.             
  309.             // Add entry for this file and word
  310.             fprintf(fp_tmpproject,"%s %d %s\n",line,(int) (KEYW_SORT_MAXCOUNT - e),savelst);
  311.             hts_primindex_size++;
  312.             //}
  313.           }
  314.         }
  315.       }
  316.       //fclose(fp);
  317.     }
  318.   }
  319.  
  320.   // Delete temp file
  321.   fclose(tmpfp);
  322.   tmpfp=NULL;
  323.  
  324.   // Clear hash table
  325.   inthash_delete(&WordIndexHash);
  326. #endif
  327.   return 1;
  328. }
  329.  
  330. /*
  331.   Sort index!
  332. */
  333. void index_finish(const char* indexpath,int mode) {
  334. #if HTS_MAKE_KEYWORD_INDEX
  335.   char** tab;
  336.   char* blk;
  337.   INTsys size;
  338.   
  339.   size=fpsize(fp_tmpproject);
  340.   if (size>0) {
  341.     //FILE* fp=fopen(concat(indexpath,"index.txt"),"rb");
  342.     if (fp_tmpproject) {
  343.       tab=(char**)malloct(sizeof(char*) * (hts_primindex_size+2) );
  344.       if (tab) {
  345.         blk = malloct(size+4);
  346.         if (blk) {
  347.           fseek(fp_tmpproject,0,SEEK_SET);
  348.           if ((INTsys)fread(blk,1,size,fp_tmpproject) == size) {
  349.             char *a=blk,*b;
  350.             int index=0;
  351.             int i;
  352.             FILE* fp;
  353.  
  354.             while( (b=strchr(a,'\n')) && (index < hts_primindex_size) ) {
  355.               tab[index++]=a;
  356.               *b='\0';
  357.               a=b+1;
  358.             }
  359.             
  360.             // Sort it!
  361.             qsort(tab,index,sizeof(char*),mystrcmp);
  362.  
  363.             // Delete fp_tmpproject
  364.             fclose(fp_tmpproject);
  365.             fp_tmpproject=NULL;
  366.  
  367.             // Write new file
  368.             if (mode == 1)      // TEXT
  369.               fp=fopen(concat(indexpath,"index.txt"),"wb");
  370.             else                // HTML
  371.               fp=fopen(concat(indexpath,"sindex.html"),"wb");
  372.             if (fp) {
  373.               char current_word[KEYW_LEN + 32];
  374.               char word[KEYW_LEN + 32];
  375.               int hit;
  376.               int total_hit=0;
  377.               int total_line=0;
  378.               int last_pos=0;
  379.               char word0='\0';
  380.               current_word[0]='\0';
  381.  
  382.               if (mode == 2) {         // HTML
  383.                 for(i=0;i<index;i++) {
  384.                   if (word0 != tab[i][0]) {
  385.                     word0 = tab[i][0];
  386.                     fprintf(fp," <a href=\"#%c\">%c</a>\r\n",word0,word0);
  387.                   }
  388.                 }
  389.                 word0='\0';
  390.                 fprintf(fp,"<br><br>\r\n");
  391.                 fprintf(fp,"<table width=\"100%%\" border=\"0\">\r\n<tr>\r\n<td>word</td>\r\n<td>location\r\n");
  392.               }
  393.  
  394.               for(i=0;i<index;i++) {
  395.                 if (sscanf(tab[i],"%s %d",word,&hit) == 2) {
  396.                   char*  a=strchr(tab[i],' ');
  397.                   if (a) a=strchr(a+1,' ');
  398.                   if (a++) {                            /* Yes, a++, not ++a :) */
  399.                     hit=KEYW_SORT_MAXCOUNT-hit;
  400.                     if (strcmp(word,current_word)) {    /* New word */
  401.                       if (total_hit) {
  402.                         if (mode == 1)      // TEXT
  403.                           fprintf(fp,"\t=%d\r\n",total_hit);
  404.                         //else                // HTML
  405.                         //  fprintf(fp,"<br>(%d total hits)\r\n",total_hit);
  406.                         if ( 
  407.                               ( ((total_hit*1000 ) / hts_primindex_words) >= KEYW_USELESS1K   )
  408.                             ||
  409.                               ( ((total_line*1000) / index              ) >= KEYW_USELESS1KPG )
  410.                           ) {
  411.                           fseek(fp,last_pos,SEEK_SET);
  412.                           if (mode == 1)      // TEXT
  413.                             fprintf(fp,"\tignored (%d)\r\n",((total_hit*1000)/hts_primindex_words));
  414.                           else
  415.                             fprintf(fp,"(ignored) [%d hits]<br>\r\n",total_hit);
  416.                         }
  417.                         else {
  418.                           if (mode == 1)      // TEXT
  419.                             fprintf(fp,"\t(%d)\r\n",((total_hit*1000)/hts_primindex_words));
  420.                           //else                // HTML
  421.                           //  fprintf(fp,"(%d)\r\n",((total_hit*1000)/hts_primindex_words));
  422.                         }
  423.                       }
  424.                       if (mode == 1)      // TEXT
  425.                         fprintf(fp,"%s\r\n",word);
  426.                       else {              // HTML
  427.                         fprintf(fp,"</td></tr>\r\n");
  428.                         if (word0 != word[0]) {
  429.                           word0 = word[0];
  430.                           fprintf(fp,"<th>%c</th>\r\n",word0);
  431.                           fprintf(fp,"<a name=\"%c\"></a>\r\n",word0);
  432.                         }
  433.                         fprintf(fp,"<tr>\r\n<td>%s</td>\r\n<td>\r\n",word);
  434.                       }
  435.                       fflush(fp); last_pos=ftell(fp);
  436.                       strcpybuff(current_word,word);
  437.                       total_hit=total_line=0;
  438.                     }
  439.                     total_hit+=hit;
  440.                     total_line++;
  441.                     if (mode == 1)      // TEXT
  442.                       fprintf(fp,"\t%d %s\r\n",hit,a);
  443.                     else                // HTML
  444.                       fprintf(fp,"<a href=\"%s\">%s</a> [%d hits]<br>\r\n",a,a,hit);
  445.                   }
  446.                 }
  447.               }
  448.               if (mode == 2)         // HTML
  449.                 fprintf(fp,"</td></tr>\r\n</table>\r\n");
  450.               fclose(fp);
  451.             }
  452.             
  453.           }
  454.           freet(blk);
  455.         }
  456.         freet(tab);
  457.       }
  458.  
  459.     }
  460.     //qsort
  461.   }
  462.   if (fp_tmpproject)
  463.     fclose(fp_tmpproject);
  464.   fp_tmpproject=NULL;
  465. #endif
  466. }
  467.  
  468.  
  469. /* Subroutines */
  470.  
  471. #if HTS_MAKE_KEYWORD_INDEX
  472. int strcpos(char* adr,char c) {
  473.   char* apos=strchr(adr,c);
  474.   if (apos)
  475.     return (int)(apos-adr);
  476.   else
  477.     return -1;
  478. }
  479.  
  480. int mystrcmp(const void* _e1,const void* _e2) {
  481.   char** e1=(char**)_e1;
  482.   char** e2=(char**)_e2;
  483.   return strcmp(*e1,*e2);
  484. }
  485. #endif
  486.  
  487.